using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using usm.SolDev.Common.Config;
using usm.SolDev.Common;

namespace usm.SolDev.UI.Sys
{
    public partial class SystemSettingsForm : usm.SolDev.UI.BaseForm
    {
        /// <summary>
        /// 
        /// </summary>
        public SystemSettingsForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SystemSettingsForm_Load(object sender, EventArgs e)
        {
            SystemSettings sysSettings = new SystemSettings();
            try
            {
                sysSettings = (SystemSettings)sysSettings.LoadFromFile(sysSettings);
            } catch (SolDev.Common.SolDevException /*ex*/)
            {
                sysSettings.SaveToFile(sysSettings);
            }

            txtHostName.Text = sysSettings.SQLConnection.HostName.ToString();
            txtDbName.Text = sysSettings.SQLConnection.DatabaseName.ToString();
            txtPort.Text = sysSettings.SQLConnection.PortNumber.ToString();
            txtDbUser.Text= sysSettings.SQLConnection.UserName.ToString();
            txtPassword.Text = sysSettings.SQLConnection.Password.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            SystemSettings sysSettings = new SystemSettings();
            sysSettings.SQLConnection = new SQLConnectionString(txtHostName.Text,
                txtDbName.Text, txtDbUser.Text, txtPassword.Text, int.Parse(txtPort.Text));

            try
            {
                StartWaiting();
                sysSettings.SaveToFile(sysSettings);
                sysSettings.SQLConnection.SaveToFile();
                DialogResult = DialogResult.OK;

            } catch (Exception ex)
            {
                new SolDevException(String.Format("Failed to save settings : {0}", ex.Message));
                EndWaiting();
            }
        }
    }
}

